package [package].controller;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import [package].model.[Table2];
import [package].service.[Table2]Service;

import com.rdxer.common.entity.PageResult;
import com.rdxer.common.entity.Result;
import com.rdxer.common.entity.StatusCode;
import com.rdxer.common.utils.search.SearchPara;

/**
 * [comment]控制器层
 */
@RestController
@CrossOrigin
@RequestMapping("/[table2]")
public class [Table2]Controller {

	@Autowired
	private [Table2]Service [table2]Service;
	
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@GetMapping
	public Result<List<[Table2]>> findAll(){
		return new Result<List<[Table2]>>(true,StatusCode.OK,"查询成功",[table2]Service.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param [key2] ID
	 * @return
	 */
	@GetMapping("/{[key2]}")
	public Result<[Table2]> findById(@PathVariable [keyType] [key2]){
		return new Result(true,StatusCode.OK,"查询成功",[table2]Service.findById([key2]));
	}



	/**
	 * 增加
	 * @param [table2]
	 */
	@PostMapping
	public Result add(@RequestBody [Table2] [table2]  ){
		Result<[Table2]> result = new Result<>();
		result.setData([table2]Service.add([table2]));
		result.setMessage("新增成功");
		return result;
	}

	/**
	 * 修改
	 * @param [table2]
	 */
	@PutMapping("/{[key2]}")
	public Result<[Table2]> update(@PathVariable [keyType] [key2], @RequestBody [Table2] [table2]){

		[table2].set[Key2]([key2]);

		[Table2] data = [table2]Service.update([table2]);

		Result<[Table2]> result = new Result<>();
		result.setData(data);
		result.setMessage("修改成功");
		return result;
	}

	/**
	 * 删除
	 * @param [key2]
	 */
	@DeleteMapping("/{[key2]}")
	public Result<?> delete(@PathVariable [keyType] [key2]){
		[table2]Service.deleteById([key2]);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	//	 * 分页+多条件查询
	@PostMapping("/search")
	public Result<Page<[Table2]>> search(@Valid @RequestBody SearchPara para){
		Result<Page<[Table2]>> result = new Result<>();
		Page<[Table2]> data = [table2]Service.search(para);
		result.setData(data);
		return result;
	}

	//	 * 不分页+多条件查询
	@PostMapping("/searchNoPage")
	public Result<List<[Table2]>> searchNoPage(@Valid @RequestBody SearchPara para){
		Result<List<[Table2]>> result = new Result<>();
		List<[Table2]> data = [table2]Service.searchNoPage(para);
		result.setData(data);
		return result;
	}
}
